<?php
/**
 * @version		$Id: orders.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelOrders extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'OrderId', 'a.OrderId',
				'OrderClient', 'a.OrderClient', 'client',
				'OrderDate', 'a.OrderDate',
				'OrderProduct', 'a.OrderProduct',
				'OrderBillingCycle', 'a.OrderBillingCycle', 'billing_cycle',
				'OrderRecurringCost', 'a.OrderRecurringCost',	
				'OrderDomain', 'a.OrderDomain',
				'OrderDomainType', 'a.OrderDomainType',
				'OrderNextBilling', 'a.OrderNextBilling',
				'OrderStatus', 'a.OrderStatus',
				'OrderCoupon', 'a.OrderCoupon',
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('site');
		
		parent::populateState('a.OrderId', 'desc');		
				
	}	
	
	protected function getListQuery() 
	{
	
		$user	= JFactory::getUser();
		$db = JFactory::getDBO();
		
		$query = $db->getQuery(true);
		$query->select('ClientId');
		$query->from('#__almh_clients');
		$query->where('ClientUser =' .$user->get('id'));
		$db->setQuery($query);
		$orderClient = $db->loadResult();
		
		
		$db = JFactory::getDBO();	
		$query = $db->getQuery(true);
		$query->select($this->getState('list.select', 'a.*'));
		$query->from('#__almh_orders AS a');
		
		if (!$orderClient) {
			$query->where('a.OrderClient = 0');
			} else {	 
			$query->where('a.OrderClient =' .$orderClient);
		} 
		$query->select('c.ClientName AS client');
		$query->join('LEFT', '#__almh_clients AS c ON c.ClientId = a.OrderClient');
		
		$query->select('b.PriceName AS billing_cycle');
		$query->join('LEFT', '#__almh_prices AS b ON b.PriceId = a.OrderBillingCycle');
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.OrderId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;
	}
}
?>